/**
 * Created by gaoyj on 2018/6/27.
 */
window.onload = function (ev) {
    topbar.hide();
};
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
if (month < 10) {
    month = "0" + month;
}
if (day < 10) {
    day = "0" + day;
}
var _start = myDate.getFullYear() + '-' + month + '-' + day + " 00:00:00";
var _end = myDate.getFullYear() + '-' + month + '-' + day + " 23:59:59";
$("#startTime").val(_start);
$("#endTime").val(_end);
laydate.render({
    elem: '#startTime',
    type: 'datetime',

});
laydate.render({
    elem: '#endTime' //指定元素
    , type: 'datetime',
});
$('#dataTable').bootstrapTable({
    url: '/UserAnnouncementController/queryAnnouncement',
    height: $(window).height() - 200,
    pageSize: 30,
    pagination: true,
    pageList: [30, 50, 100],
    clickToSelect: true,
    singleSelect: true,
    queryParams: queryParams,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "total": res.total,//总页数
            "rows": res.resultList   //数据
        };
    },
    columns: [
        {
            checkbox: true
        },
        {
            width: '3px',
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        }, {
            title: '标题',
            field: 'title',
            width: 150,
            align: 'center'
        },
        {
            title: '作者',
            field: 'author',
            width: 50,
            align: 'center'
        },
        {
            title: '发布人',
            field: 'admin',
            width: 50,
            align: 'center'
        },
        {
            title: '发布时间',
            field: 'date',
            width: 100,
            align: 'center'
        },
        {
            title: '状态',
            field: 'status',
            width: 70,
            align: 'center',
            formatter: function (v, row, index) {
                if (v == "0") {
                    return "正常"
                } else {
                    return "已删除"
                }
            }
        },
        {
            title: '会员模板',
            field: 'modelName',
            width: 80,
            align: 'center'
        },
        {
            title: '操作',
            field: '',
            width: 60,
            align: 'center',
            formatter: function (v, row, index) {

                return "<a href='javascript:void(0)' onclick='editAnnouncement(" + row.id + ")' > 编辑 </a> &nbsp; " +
                    "<a href='javascript:void(0)' onclick='deleteAnnouncement(" + row.id + ")' > 删除 </a>";
            }
        }
    ]
})

$("#search").click(function () {
    var timeAdd = new Date($("#startTime").val());
    var timeEnd = new Date($("#endTime").val());
    if (timeAdd > timeEnd) {
        layer.msg("开始时间不能大于结束时间");
        return false;
    }
    $("#dataTable").bootstrapTable("refresh");
});
function queryParams(params) {
    return {
        size: params.pageSize,
        page: params.pageNumber,
        beginTime: $("#startTime").val(),
        endTime: $("#endTime").val()
    };
}

function editAnnouncement(v) {

    addTab('/writeAnnouncement?id=' + v, '1', '修改公告')
}

function deleteAnnouncement(v) {
    $.confirm({
        title: '确认!',
        content: '确定删除这条公告？',
        confirm: function () {
            $.post("/UserAnnouncementController/updateAnnouncement?id="
                + v + "&operate=1", function (data) {
                layer.msg(data.message);
                $("#dataTable").bootstrapTable("refresh");
            });
        },
        cancel: function () {

        }
    });
}



